// platform.ts

import { httpTauri } from "@/utils/http";
import { localServer } from "@/api/utils";
import { PageResult, Result } from "@/api/common";

export interface Experiment {
  id: string;
  name: string;
  desc: string;
  state: number;
  expPlatformId: string;
  sensors: any[]; // Replace 'any' with the actual type if known
  startTime: string | null;
  endTime: string | null;
  canChangeParam: boolean;
  days: number;
  hours: number;
  minutes: number;
  seconds: number;
  createUser: string;
  createTime: string;
  updateUser: string;
  updateTime: string;
  ext: string;
}

export const listPageExperiments = async (params: object) => {
  return httpTauri.request<PageResult<Array<Experiment>>>(
    "get",
    localServer("/api/experiment/instance/listAll"),
    { params }
  );
};

export const listExperiments = async (params: object) => {
  return httpTauri.request<PageResult<Array<Experiment>>>(
    "get",
    localServer("/api/experiment/instance/list"),
    { params }
  );
};

export const getExperiment = async (expId: string) => {
  return httpTauri.request<Result<Experiment>>(
    "get",
    localServer(`/api/experiment/instance/get?id=${expId}`)
  );
};

export const addInstance = async (data: Experiment) => {
  return httpTauri.request<Result<Experiment>>(
    "post",
    localServer(`/api/experiment/instance/add`),
    { data }
  );
};

export const updateInstance = async (data: Experiment) => {
  return httpTauri.request<Result<Experiment>>(
    "post",
    localServer(`/api/experiment/instance/update`),
    { data }
  );
};

export const updateInstanceExt = async (data: Experiment) => {
  return httpTauri.request<Result<Experiment>>(
    "post",
    localServer(`/api/experiment/instance/updateExt`),
    { data }
  );
};

export const startInstance = async id => {
  const data = { id: id };
  return httpTauri.request<Result<string>>(
    "post",
    localServer(`/api/experiment/instance/start`),
    { data }
  );
};

export const stopInstance = async id => {
  const data = { id: id };
  return httpTauri.request<Result<string>>(
    "post",
    localServer(`/api/experiment/instance/stop`),
    { data }
  );
};

export const deleteInstance = async id => {
  const data = { id: id };
  return httpTauri.request<Result<string>>(
    "post",
    localServer(`/api/experiment/instance/delete`),
    { data }
  );
};

export const getRunningExperiments = async () => {
  return httpTauri.request<PageResult<Array<Experiment>>>(
    "get",
    localServer("/api/experiment/instance/getrunning")
  );
};

export const removeExpDevice = async data => {
  return httpTauri.request<Result<string>>(
    "post",
    localServer("/api/experiment/platform/device/remove"),
    { data }
  );
};
